package com.ygp.jmm.ordering;

import com.ygp.utils.Sleeper;
import lombok.extern.slf4j.Slf4j;

@Slf4j(topic = "c.Ordering")
public class Ordering {
    private  static int x = 0, y = 0;
    private  static int a = 0, b = 0;

    public static void main(String[] args) throws InterruptedException {
        int i = 0;
        for (;;){
            Sleeper.sleep(0.001);
            i++;
            x = 0; y = 0;
            a = 0; b = 0;
            Thread t1 = new Thread(() -> {
                a = 1;
                x = b;
            });

            Thread t2 = new Thread(() -> {
                b = 1;
                y = a;
            });
            t1.start();
            t2.start();
            t1.join();
            t2.join();
            String result = "第" + i + "次 (" + x + "," + y + "）";
            log.info(result);
            if(x == 0 && y == 0) {
                break;
            } else {
                log.info(result);
            }
        }
    }
}
